package com.xunshangwang.pinhaohuo.widgets;

import android.content.Context;
import android.graphics.PorterDuff;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.xunshangwang.pinhaohuo.R;

import java.util.List;

import tool.library.adapter.BaseRecyclerAdapter;
import tool.library.widget.LoadRecyclerView;

/**
 * Created by xzp on 15/9/23.
 */
public class LMPageRecyclerView extends LoadRecyclerView implements View.OnClickListener{
    private LoadHolder mHolder;
    private int mPage;
    private String mCompleteMsg;
    private int mCount;
    public LMPageRecyclerView(Context context) {
        super(context);
        init();

    }

    public LMPageRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LMPageRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }
    private void init(){
        mCompleteMsg="暂无更多内容";
    }

    public void setCompleteMsg(String completeMsg) {
        this.mCompleteMsg = completeMsg;
    }

    @Override
    protected ViewHolder initLoadHolder(Context context) {
        View view= LayoutInflater.from(context).inflate(R.layout.load_more,null);
        ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(params);
        mHolder=new LoadHolder(view);
        mHolder.progressBar.getIndeterminateDrawable().setColorFilter(
                ContextCompat.getColor(context, R.color.red), PorterDuff.Mode.SRC_IN);
        view.setOnClickListener(this);
        mPage=0;
        return mHolder;
    }

    public void setCount(int count) {
        this.mCount = count;
    }

    public void loadFinish(boolean success)
    {
        if(success) {
            if (getAdapter() instanceof BaseRecyclerAdapter) {
                BaseRecyclerAdapter adapter = (BaseRecyclerAdapter) getAdapter();
                if(mCount>adapter.getListSize())
                    loadFinish();
                else
                    loadComplete();
            }
            mPage++;
        }
        else {
            loadFinish();
        }
    }
    public void resetPage()
    {
        mPage=0;
        loadFinish(true);
    }
    public int getPage()
    {
        return mPage;
    }
    @Override
    public void onClick(View v) {
        if(getLoadListener()!=null)
        {
            getLoadListener().onLoad(this);
            changeState(LOAD_START);
        }
    }

    @Override
    protected void onInit() {
        mHolder.itemView.setVisibility(GONE);
    }

    @Override
    protected void onStart() {
        mHolder.itemView.setVisibility(VISIBLE);
        mHolder.itemView.setEnabled(false);
        mHolder.msgText.setVisibility(INVISIBLE);
        mHolder.progressBar.setVisibility(VISIBLE);
    }

    @Override
    protected void onFinish() {
        mHolder.itemView.setVisibility(VISIBLE);
        mHolder.itemView.setEnabled(true);
        mHolder.msgText.setVisibility(VISIBLE);
        mHolder.msgText.setText("点击加载更多");
        mHolder.progressBar.setVisibility(INVISIBLE);
    }

    @Override
    protected void onComplete() {
        mHolder.itemView.setVisibility(VISIBLE);
        mHolder.itemView.setEnabled(false);
        mHolder.msgText.setVisibility(VISIBLE);
        mHolder.msgText.setText(mCompleteMsg);
        mHolder.progressBar.setVisibility(INVISIBLE);
    }
    protected class LoadHolder extends ViewHolder{
        private ProgressBar progressBar;
        private TextView msgText;
        //private View loadView;
        public LoadHolder(View itemView) {
            super(itemView);
           // loadView=itemView;
            progressBar= (ProgressBar) itemView.findViewById(R.id.load_progress);
            msgText= (TextView) itemView.findViewById(R.id.load_msg_text);
        }
    }
}
